export function randomInt(inf: number, sup: number): number {
  return Math.floor(Math.random() * (sup - inf)) + inf;
}

export function randomStringWithCharset(len: number, charset: string): string {
  if (charset.length === 0) {
    throw new Error('charset of random string cannot be empty');
  }

  if (len <= 0) {
    throw new Error('len of random string should be greater than 0');
  }

  const set = [];
  for (let i = 0; i < len; i++) {
    set.push(charset[randomInt(0, charset.length)]);
  }
  return set.join('');
}

const RANDOM_STRING_BASE = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

export function randomString(len: number): string {
  return randomStringWithCharset(len, RANDOM_STRING_BASE);
}
